package com.zhuss.job.job;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.job.job.DetailJobBo;
import com.zhuss.bean.bo.job.job.SearchJobBo;
import com.zhuss.bean.bo.job.job.wechat.ListJobBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.job.job.DetailJobVo;
import com.zhuss.bean.vo.job.job.ListJobVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.job.processor.JobAppendRead;
import com.zhuss.job.processor.JobRead;
import com.zhuss.job.processor.JobTopicRead;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;


/**
 * job查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("jobReadService")
public class JobReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("jobRead")
	private JobRead jobRead;

	@Autowired
	@Qualifier("jobTopicRead")
	private JobTopicRead jobTopicRead;
	
	@Autowired
	@Qualifier("jobAppendRead")
	private JobAppendRead jobAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailJobBo bo = (DetailJobBo) param;
		Long jobId = bo.getJobId();
		DetailJobVo vo = (DetailJobVo) jobRead.detail(jobId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchJobBo bo = (SearchJobBo) param;
		Object result = jobRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailJobBo param) throws WpsException {
		Long jobId = param.getJobId();
		Object result = jobTopicRead.listTopicTree(jobId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listJob(Object param) throws WpsException {
		ListJobBo bo = (ListJobBo) param;
		Object result = jobRead.listJob(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailJobVo> pageVo = (PageVo<DetailJobVo>) result;
		for (DetailJobVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = jobAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getJobId(), null).size();
			Integer collectNum = jobAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getJobId(), null).size();
			Integer forwardNum = jobAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getJobId(), null).size();
			Integer topicNum = jobTopicRead.listTopic(vo.getJobId(), 0l).size();
			
			Integer thumbSelfNum = jobAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getJobId(), userId).size();
			Integer collectSelfNum = jobAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getJobId(), userId).size();
			Integer forwardSelfNum = jobAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getJobId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListJobVo> pageVo = (PageVo<ListJobVo>) result;
		for (ListJobVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = jobAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getJobId(), null).size();
			Integer collectNum = jobAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getJobId(), null).size();
			Integer forwardNum = jobAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getJobId(), null).size();
			Integer topicNum = jobTopicRead.listTopic(vo.getJobId(), 0l).size();
			
			Integer thumbSelfNum = jobAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getJobId(), userId).size();
			Integer collectSelfNum = jobAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getJobId(), userId).size();
			Integer forwardSelfNum = jobAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getJobId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			
		}
	}
	
	
}

